fractions

您所在的位置:网站首页 decimal number和fraction fractions

fractions

2023-05-01 20:55| 来源: 网络整理| 查看: 265

第一个版本要求 numerator 和 denominator 是 numbers.Rational 的实例,并返回一个新的 Fraction 实例,其值为 numerator/denominator。 如果 denominator 为 0 将会引发 ZeroDivisionError。 第二个版本要求 other_fraction 是 numbers.Rational 的实例,并返回一个 Fraction 实例且与传入值相等。 下两个版本接受 float 或 decimal.Decimal 的实例,并返回一个 Fraction 实例且与传入值完全相等。 请注意由于二进制浮点数通常存在的问题 (参见 浮点算术:争议和限制),Fraction(1.1) 的参数并不会精确等于 11/10,因此 Fraction(1.1) 也 不会 返回用户所期望的 Fraction(11, 10)。 (请参阅下文中 limit_denominator() 方法的文档。) 构造器的最后一个版本接受一个字符串或 unicode 实例。 此实例的通常形式为:

[sign] numerator ['/' denominator]

where the optional sign may be either '+' or '-' and numerator and denominator (if present) are strings of decimal digits (underscores may be used to delimit digits as with integral literals in code). In addition, any string that represents a finite value and is accepted by the float constructor is also accepted by the Fraction constructor. In either form the input string may also have leading and/or trailing whitespace. Here are some examples:

>>> from fractions import Fraction >>> Fraction(16, -10) Fraction(-8, 5) >>> Fraction(123) Fraction(123, 1) >>> Fraction() Fraction(0, 1) >>> Fraction('3/7') Fraction(3, 7) >>> Fraction(' -3/7 ') Fraction(-3, 7) >>> Fraction('1.414213 \t\n') Fraction(1414213, 1000000) >>> Fraction('-.125') Fraction(-1, 8) >>> Fraction('7e-6') Fraction(7, 1000000) >>> Fraction(2.25) Fraction(9, 4) >>> Fraction(1.1) Fraction(2476979795053773, 2251799813685248) >>> from decimal import Decimal >>> Fraction(Decimal('1.1')) Fraction(11, 10)

The Fraction class inherits from the abstract base class numbers.Rational, and implements all of the methods and operations from that class. Fraction instances are hashable, and should be treated as immutable. In addition, Fraction has the following properties and methods:

在 3.2 版更改: Fraction 构造器现在接受 float 和 decimal.Decimal 实例。

在 3.9 版更改: 现在会使用 math.gcd() 函数来正规化 numerator 和 denominator。 math.gcd() 总是返回 int 类型。 在之前版本中,GCD 的类型取决于 numerator 和 denominator 的类型。

在 3.11 版更改: Underscores are now permitted when creating a Fraction instance from a string, following PEP 515 rules.

在 3.11 版更改: Fraction implements __int__ now to satisfy typing.SupportsInt instance checks.

numerator¶

最简分数形式的分子。

denominator¶

最简分数形式的分母。

as_integer_ratio()¶

返回由两个整数组成的元组,两数之比等于该分数的值且其分母为正数。

3.8 新版功能.

classmethod from_float(flt)¶

只接受 float 或 numbers.Integral 实例的替代性构造器。 请注意 Fraction.from_float(0.3) 与 Fraction(3, 10) 的值是不同的。

备注

从 Python 3.2 开始,在构造 Fraction 实例时可以直接使用 float。

classmethod from_decimal(dec)¶

只接受 decimal.Decimal 或 numbers.Integral 实例的替代性构造器。

备注

从 Python 3.2 开始,在构造 Fraction 实例时可以直接使用 decimal.Decimal 实例。

limit_denominator(max_denominator=1000000)¶

找到并返回一个 Fraction 使得其值最接近 self 并且分母不大于 max_denominator。 此方法适用于找出给定浮点数的有理数近似值:

>>> from fractions import Fraction >>> Fraction('3.1415926535897932').limit_denominator(1000) Fraction(355, 113)

或是用来恢复被表示为一个浮点数的有理数:

>>> from math import pi, cos >>> Fraction(cos(pi/3)) Fraction(4503599627370497, 9007199254740992) >>> Fraction(cos(pi/3)).limit_denominator() Fraction(1, 2) >>> Fraction(1.1).limit_denominator() Fraction(11, 10) __floor__()¶

返回最大的 int >> from math import floor >>> floor(Fraction(355, 113)) 3 __ceil__()¶

返回最小的 int >= self。 此方法也可通过 math.ceil() 函数来使用。

__round__()¶ __round__(ndigits)

第一个版本返回一个 int 使得其值最接近 self,位值为二分之一时只对偶数舍入。第二个版本会将 self 舍入到最接近 Fraction(1, 10**ndigits) 的倍数(如果 ndigits 为负值则为逻辑运算),位值为二分之一时同样只对偶数舍入。 此方法也可通过 round() 函数来使用。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3